package com.hnkywl.sts.webservice.bg.impl;

import com.hnkywl.sts.service.base.WebserviceLogService;
import com.hnkywl.sts.service.htgl.BghtService;
import com.hnkywl.sts.webservice.bg.IContractForbid;
import com.hnkywl.sts.webservice.bg.request.ContractForbid;
import com.hnkywl.sts.webservice.bg.response.Response;
import com.hnkywl.sts.webservice.bg.util.ConstantBg;
import com.hnkywl.sts.webservice.bg.util.StackTraceUtil;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: nianjunyan
 * Date: 13-5-21
 * Time: 下午9:15
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ContractForbidService implements IContractForbid, ApplicationContextAware {
    private ApplicationContext applicationContext;
    // 保供合同service
    private BghtService bghtService;
    private WebserviceLogService webserviceLogService;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
        bghtService = (BghtService) applicationContext.getBean("bghtService");
        webserviceLogService = (WebserviceLogService) applicationContext.getBean("webserviceLogService");
    }

    public Response docontractforbid(String contractForbidXML) {
        // 返回值
        Response res = new Response();
        String exception = "";
        List<ContractForbid> contractForbidList = getContractForbids(contractForbidXML);
        if (null == contractForbidList || contractForbidList.size() <= 0) {
            res.setRetCode(ConstantBg.LOSE_PARA);
            res.setRemark("缺失参数或者参数为空！");
        } else {
            // 进行数据处理，调用service里的方法
            try {
                String reslut = this.bghtService.InterfaceContractForbid(contractForbidList);
                if (null == reslut) {
                    res.setRetCode(ConstantBg.OPERATOR_ILLEGAL);
                    res.setRemark("数据操作异常！");
                } else {
                    res.setRetCode(ConstantBg.SYN_SUCCESS);
                    res.setRemark("数据同步成功！");
                }
            } catch (Exception e) {
                e.printStackTrace();
                exception = StackTraceUtil.getStackTrace(e);
                res.setRetCode(ConstantBg.DEAL_FAIL);
                res.setRemark("数据处理错误！");
            }
        }
        webserviceLogService.saveServerLog(
                ContractForbidService.class.getName(),
                "docontractforbid",
                contractForbidXML,
                "返回代码：" + res.getRetCode() + "  说明：" + res.getRemark(),
                exception,
                ""
        );
        return res;
    }

    /**
     * 解析xml转化为list
     *
     * @param contractForbidXML
     * @return
     */
    private List<ContractForbid> getContractForbids(String contractForbidXML) {
        List<ContractForbid> list = new ArrayList<ContractForbid>();
        ContractForbid contractForbid;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder;
            builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new ByteArrayInputStream(contractForbidXML.getBytes()));
            int nodeLength = doc.getElementsByTagName("contract").getLength();
            for (int i = 0; i < nodeLength; i++) {
                contractForbid = new ContractForbid();
                String contractDetailId = doc.getElementsByTagName("contractdetailid").item(i).getFirstChild().getNodeValue();
                String stopFlag = doc.getElementsByTagName("stopflag").item(i).getFirstChild().getNodeValue();
                contractForbid.setContractDetailId(Long.valueOf(contractDetailId));
                contractForbid.setStopflag(stopFlag);
                list.add(contractForbid);
            }

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return list;
    }
}
